package com.ilearn.iodata.iolingo.persistent.db;

import com.ilearn.iodata.iolingo.service.GetCollectionService;
import com.ilearn.iodata.iolingo.service.GetEntityService;
import com.ilearn.iodata.iolingo.service.GetPrimitiveService;
import org.apache.olingo.commons.api.data.Entity;
import org.apache.olingo.commons.api.data.EntityCollection;
import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.edm.EdmEntitySet;
import org.apache.olingo.commons.api.edm.EdmKeyPropertyRef;
import org.apache.olingo.commons.api.edm.EdmProperty;
import org.apache.olingo.server.api.uri.UriParameter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class MySQLStorage extends DbStorage {

    @Autowired
    private GetCollectionService getCollectionService;

    @Autowired
    private GetEntityService getEntityService;

    @Autowired
    private GetPrimitiveService getPrimitiveService;

    @Override
    public EntityCollection getCollection(EdmEntitySet edmEntitySet) {
        EntityCollection entityCollection = new EntityCollection();

        List<Entity> entities = entityCollection.getEntities();

        StringBuilder sql = new StringBuilder();
        sql.append("select * from ");

        String entitySetName = edmEntitySet.getName();

        sql.append(entitySetName);

        String sqlStatement = sql.toString();

        System.out.println(">>>>>>>>sql>>>>>>>>> " + sqlStatement + " <<<<<<<<<<<<<<<<<<");

        entities.addAll(getCollectionService.getCollectionByJdbcTemplate(sqlStatement, entitySetName, edmEntitySet.getEntityType().getKeyPropertyRefs()));

        return entityCollection;
    }

    @Override
    public Entity getEntityByKey(EdmEntitySet edmEntitySet, List<UriParameter> keyPredicates) {
        List<EdmKeyPropertyRef> keyPropertyRefs = edmEntitySet.getEntityType().getKeyPropertyRefs();


        String entitySetName = edmEntitySet.getName();

        StringBuilder sql = new StringBuilder();
        sql.append("select * from ");
        sql.append(entitySetName);
        sql.append(" where 1=1");

        for (UriParameter keyPredicate : keyPredicates) {
            EdmKeyPropertyRef keyPropertyRef = edmEntitySet.getEntityType().getKeyPropertyRef(keyPredicate.getName());
            keyPropertyRef.getProperty().getType().getKind();
            sql.append(" and " + keyPredicate.getName() + "=" + keyPredicate.getText()); // + keyPredicate.getText()
        }
        String sqlStatement = sql.toString();
        System.out.println("》》》》》》》》》 " + sqlStatement + " 《《《《《《《《《《《《《《《");
        Entity entity = getEntityService.getEntityByJdbcTemplate(sqlStatement, entitySetName, keyPropertyRefs);
        return entity;
    }

    @Override
    public Property getPrimitiveByKey(EdmEntitySet edmEntitySet, List<UriParameter> keyPredicates, EdmProperty edmProperty) {
        // 用于传输到后面生成URI的createId使用
        List<EdmKeyPropertyRef> keyPropertyRefs = edmEntitySet.getEntityType().getKeyPropertyRefs();


        String entitySetName = edmEntitySet.getName();

        StringBuilder sql = new StringBuilder();
        sql.append("select ");
        sql.append(edmProperty.getName());
        sql.append(" from ");
        sql.append(entitySetName);
        sql.append(" where 1=1");

        for (UriParameter keyPredicate : keyPredicates) {
            sql.append(" and " + keyPredicate.getName() + "=" + keyPredicate.getText()); // + keyPredicate.getText()
        }
        String sqlStatement = sql.toString();
        System.out.println("》》》》》》》》》 " + sqlStatement + " 《《《《《《《《《《《《《《《");
        Property primitive = getPrimitiveService.getPrimitiveByJdbcTemplate(sqlStatement, entitySetName, keyPropertyRefs);
        return primitive;
    }
}
